package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.RiseStudent;
import cc.mrbird.system.domain.Student;
import cc.mrbird.web.service.RiseStudentService;
import cc.mrbird.web.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class RiseStudentController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(RiseStudentController.class);

	@Autowired
	private RiseStudentService reseStudentService;

	@Value("${sys.admin}")
	private String admin;

	@Log("获取考生信息")
	@RequestMapping("rise_student")
	@RequiresPermissions("rise_student:list")
	public String index() {
		return "web/rise_student/rise_student";
	}

	@RequestMapping("rise_student/list")
	@ResponseBody
	public Map<String, Object> studentList(QueryRequest request, RiseStudent riseStudent, String sonUserName) {
		List<RiseStudent> list;
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		if(riseStudent.getUserName().equalsIgnoreCase(admin) && sonUserName == null){
			riseStudent.setUserName("");
			list = this.reseStudentService.findRiseStudentByCondition(riseStudent);
		}else if(riseStudent.getUserName().equalsIgnoreCase(admin) && sonUserName != null){
			riseStudent.setUserName(sonUserName);
			list = this.reseStudentService.findRiseStudentByCondition(riseStudent);
		}else {
			list = this.reseStudentService.findRiseStudentByCondition(riseStudent);
		}
		PageInfo<RiseStudent> pageInfo = new PageInfo<RiseStudent>(list);
		return getDataTable(pageInfo);
	}

	@Log("新增考生信息")
	@RequiresPermissions("rise_student:add")
	@RequestMapping("rise_student/add")
	@ResponseBody
	public ResponseBo addStudent(RiseStudent riseStudent,String riseUserName) {
		try {
			this.reseStudentService.addRiseStudent(riseStudent,riseUserName);
			return ResponseBo.ok("新增考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增考生失败，请联系网站管理员！");
		}
	}

	@Log("删除考生")
	@RequiresPermissions("rise_student:delete")
	@RequestMapping("rise_student/delete")
	@ResponseBody
	public ResponseBo deleteStudents(String ids) {
		try {
			this.reseStudentService.deleteRiseStudents(ids);
			return ResponseBo.ok("删除考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除考生失败，请联系网站管理员！");
		}
	}

	@RequestMapping("rise_student/getStudent")
	@ResponseBody
	public ResponseBo getUser(String riseStudentId) {
		try {
			RiseStudent riseStudent = this.reseStudentService.findById(riseStudentId);
			return ResponseBo.ok(riseStudent);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取用户信息失败，请联系网站管理员！");
		}
	}

	@Log("修改考生信息")
	@RequiresPermissions("rise_student:update")
	@RequestMapping("rise_student/update")
	@ResponseBody
	public ResponseBo updateStudent(RiseStudent riseStudent) {
		try {
			this.reseStudentService.updateRiseStudent(riseStudent);
			return ResponseBo.ok("修改考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改考生失败，请联系网站管理员！");
		}
	}

	@RequestMapping("riseStudent/checkRiseStudentId")
	@ResponseBody
	public boolean checkStudentId(String riseStudentId) {
		RiseStudent result = this.reseStudentService.findById(riseStudentId);
		if (result != null)
			return false;
		return true;
	}

}
